package com.oa.ylms.apps.business.leave.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.leave.ILeaveInfoService;
import com.oa.ylms.apps.business.plan.ICheckPlanService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.IPlanCategory;
import com.oa.ylms.apps.dao.leave.ILeaveInfoDao;
import com.oa.ylms.apps.entity.archives.ArchiveInfoValue;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.leave.LeaveInfoValue;
import com.oa.ylms.apps.entity.plan.CheckPlanValue;

public class LeaveInfoServiceImpl implements ILeaveInfoService {

	private ILeaveInfoDao dao;
	private LeaveInfoValue leaveInfo;
	private ArchiveInfoValue archiveInfo;
	private StaffInfoValue staffInfo;
	private IStaffInfoService staffInfoService;
	private ICheckPlanService checkPlanService;

	public int addLeaveInfo(StaffInfoValue staffInfo, String title, String reason,
			Date startTime, Date endTime) throws Exception {
		leaveInfo.setStaffInfo(staffInfo);
		leaveInfo.setTitle(title);
		leaveInfo.setReason(reason);
		leaveInfo.setStartTime(startTime);
		leaveInfo.setEndTime(endTime);
		int postId = staffInfo.getArchiveInfo().getPostInfo().getPostId();
		List<CheckPlanValue> list = checkPlanService.queryCheckPlanWithPage(
			IPlanCategory.LEAVECHECK, postId, 10, 1);
		leaveInfo.setCheckPlan(list.get(0));
		CheckPlanValue plan= checkPlanService.queryByPostIdAndCategoryId(IPlanCategory.LEAVECHECK,postId);
		String[] deptNo=null;
		if(plan!=null){
		   deptNo=plan.getPlanOrders().split(",");
		}
		leaveInfo.setStates(Integer.parseInt(deptNo[0]));
		return dao.addLeaveInfo(leaveInfo);
	}

	public String deleteLeaveInfo(String leaveIds) throws Exception {
		String unDelIds = "";
		String[] arrayLeaveId = leaveIds.split(",");
		for (String leaveIdStr : arrayLeaveId) {
			int leaveId = Integer.parseInt(leaveIdStr);
			leaveInfo = queryLeaveInfoByLeaveId(leaveId);
			boolean result = dao.deleteLeaveInfo(leaveInfo);
			if (result == false) {
				unDelIds = leaveId + ",";
				continue;
			}
		}
		return unDelIds;
	}

	public boolean deleteLeaveInfo(int leaveId) throws Exception {
		leaveInfo = dao.queryLeaveInfoByLeaveId(leaveId);
		return dao.deleteLeaveInfo(leaveInfo);
	}

	public List<LeaveInfoValue> queryLeaveInfoByChairmainWithPage(
			String staffName, String title, int rows, int pageNo)
			throws Exception {
		if (StringUtils.isNotEmpty(staffName)
				&& StringUtils.isNotBlank(staffName)) {
			archiveInfo.setStaffName(staffName);
		}
		if (StringUtils.isNotEmpty(title) && StringUtils.isNotBlank(title)) {
			leaveInfo.setTitle(title);
		}
		archiveInfo.setStates(IDEDStates.ENABLE);
		leaveInfo.setStates(IDEDStates.ENABLE);
		return dao.queryLeaveInfoByChairmanWithPage(archiveInfo, leaveInfo,
				rows, pageNo);
	}

	public List<LeaveInfoValue> queryLeaveInfoByMinisterWithPage(int sectionId,
			String staffName, String title, int rows, int pageNo)
			throws Exception {
		if (StringUtils.isNotEmpty(staffName)
				&& StringUtils.isNotBlank(staffName)) {
			archiveInfo.setStaffName(staffName);
		}
		if (StringUtils.isNotEmpty(title) && StringUtils.isNotBlank(title)) {
			leaveInfo.setTitle(title);
		}
		archiveInfo.setStates(IDEDStates.ENABLE);
		leaveInfo.setStates(IDEDStates.ENABLE);
		return dao.queryLeaveInfoByMinisterWithPage(sectionId, archiveInfo,
				leaveInfo, rows, pageNo);
	}

	public LeaveInfoValue queryLeaveInfoByLeaveId(int leaveId) throws Exception {

		return dao.queryLeaveInfoByLeaveId(leaveId);
	}

	public int countTpagesByChairman(String staffName, String title, int rows)
			throws Exception {
		if (StringUtils.isNotEmpty(staffName)
				&& StringUtils.isNotBlank(staffName)) {
			archiveInfo.setStaffName(staffName);
		}
		if (StringUtils.isNotEmpty(title) && StringUtils.isNotBlank(title)) {
			leaveInfo.setTitle(title);
		}
		archiveInfo.setStates(IDEDStates.ENABLE);
		leaveInfo.setStates(IDEDStates.ENABLE);
		return dao.countTpagesByChairman(archiveInfo, leaveInfo, rows);
	}

	public int countTpagesByStaffId(StaffInfoValue staffInfo, int rows) throws Exception {
		
		return dao.countTpagesByStaffId(staffInfo, rows);
	}

	public int countTpagesByMinister(int sectionId, String staffName,
			String title, int rows) throws Exception {
		if (StringUtils.isNotEmpty(staffName)
				&& StringUtils.isNotBlank(staffName)) {
			archiveInfo.setStaffName(staffName);
		}
		if (StringUtils.isNotEmpty(title) && StringUtils.isNotBlank(title)) {
			leaveInfo.setTitle(title);
		}
		archiveInfo.setStates(IDEDStates.ENABLE);
		leaveInfo.setStates(IDEDStates.ENABLE);
		return dao.countTpagesByMinister(sectionId, archiveInfo, leaveInfo,
				rows);
	}

	public List<LeaveInfoValue> queryLeaveInfoByStaffIdWithPage(StaffInfoValue staffInfo,
			int rows, int pageNo) throws Exception {
		
		return dao.queryLeaveInfoByStaffIdWithPage(staffInfo, rows, pageNo);
	}

	public int countTpagesByStaffId(int staffId, int states, int rows)
			throws Exception {
		return dao.countTpagesByStaffId(staffId, states, rows);
	}

	public List<LeaveInfoValue> queryLeaveInfoByStaffIdWithPage(int staffId,
			int states, int rows, int pageNo) throws Exception {
		return dao.queryLeaveInfoByStaffIdWithPage(staffId, states, rows,
				pageNo);
	}

	public int getTotalNumber() throws Exception {

		return dao.getTotalNumber();
	}

	public ILeaveInfoDao getDao() {
		return dao;
	}

	public void setDao(ILeaveInfoDao dao) {
		this.dao = dao;
	}

	public LeaveInfoValue getLeaveInfo() {
		return leaveInfo;
	}

	public void setLeaveInfo(LeaveInfoValue leaveInfo) {
		this.leaveInfo = leaveInfo;
	}

	public IStaffInfoService getStaffInfoService() {
		return staffInfoService;
	}

	public void setStaffInfoService(IStaffInfoService staffInfoService) {
		this.staffInfoService = staffInfoService;
	}

	public ArchiveInfoValue getArchiveInfo() {
		return archiveInfo;
	}

	public void setArchiveInfo(ArchiveInfoValue archiveInfo) {
		this.archiveInfo = archiveInfo;
	}

	public ICheckPlanService getCheckPlanService() {
		return checkPlanService;
	}

	public void setCheckPlanService(ICheckPlanService checkPlanService) {
		this.checkPlanService = checkPlanService;
	}

	public StaffInfoValue getStaffInfo() {
		return staffInfo;
	}

	public void setStaffInfo(StaffInfoValue staffInfo) {
		this.staffInfo = staffInfo;
	}


}
